//
//  KBTabBar.swift
//  VVeibo110
//
//  Created by kongbin on 16/8/9.
//  Copyright © 2016年 kongbin. All rights reserved.
//

import UIKit
//声明代理方法
protocol KBTabBarDelegate :NSObjectProtocol {
    
    func btnClickin()
    
    
}

class KBTabBar: UITabBar {
    //声明代理属性
   weak var kbDelegate :KBTabBarDelegate?

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        intoUI()
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加按钮到视图中
  private  func intoUI(){
    
    addSubview(button)
    
    }
    
    //设置中间按钮的frame
   override func layoutSubviews() {
        super.layoutSubviews()
    //设置系统tabbar上的按钮位置
    
    let childW = self.frame.width * 0.2
    var index :CGFloat = 0
    
    for value in self.subviews{
    
        if value .isKindOfClass(NSClassFromString("UITabBarButton")! ){
        
        value.frame.size.width = childW
            value.frame.origin.x = childW * index
            
            index++
            
            if index == 2 {
            
            index++
            
            }
        
        }
    
    }
    
    
    button.center.x = self.frame.width*0.5
    button.center.y = self.frame.height*0.5
    
    }
    
    
    //懒加载按钮
    private lazy var button :UIButton = {
    
        let btn = UIButton()
        
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), forState: UIControlState.Selected)
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), forState: UIControlState.Selected)
        
        btn.addTarget(self, action: "btnClick", forControlEvents: UIControlEvents.TouchUpInside)
        btn.sizeToFit()
        return btn

    }()
    
    //按钮点击事件
    func btnClick() -> () {
   //调用代理
kbDelegate?.btnClickin()
        
    }
    
}
